package com.kaishengit.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.ImDao;
import com.kaishengit.pojo.Company;
import com.kaishengit.pojo.Contacts;
import com.kaishengit.pojo.Im;
import com.kaishengit.util.DateUtil;

@Transactional
public class ImService {
		
	private ImDao imDao;

	public void saveim(String[] im, String[] imtype, Contacts contacts) {
		imDao.delAll(findByObject(contacts));
		if(im != null && imtype != null) {
			
			for(int i = 0; i < im.length; i ++) {
				for(int j = 0; j < imtype.length; j ++) {
					if(i == j) {
						Im ims = new Im();
						ims.setIm(im[i]);
						ims.setType(imtype[j]);
						ims.setCreatetime(DateUtil.getNow());
						ims.setContacts(contacts);
						imDao.save(ims);
					}
				}
			}
		}
	}

	public ImDao getImDao() {
		return imDao;
	}

	public void setImDao(ImDao imDao) {
		this.imDao = imDao;
	}

	public void saveim(String[] im, String[] imtype, Company company) {
		if(im != null && imtype != null) {
			
			for(int i = 0; i < im.length; i ++) {
				for(int j = 0; j < imtype.length; j ++) {
					if(i == j) {
						Im ims = new Im();
						ims.setIm(im[i]);
						ims.setType(imtype[j]);
						ims.setCreatetime(DateUtil.getNow());
						ims.setCompany(company);
						imDao.save(ims);
					}
				}
			}
		}
			
	}

	public List<Im> findByObject(Contacts contacts) {
			String hql = "from Im where im.contacts.id =?";
			return imDao.find(hql, contacts.getId());
		}

	public List<Im> findByObject(Company company) {
		String hql = "from Im where im.contacts.id = ?";
		return imDao.find(hql, company.getId());
	}

	public void delim(Contacts contacts) {
		imDao.delAll(findByObject(contacts));
	}
	}

	
	
	
